package TankGame;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

// 敌方坦克类
class EnemyTank extends Tank {
    private int health;
    private Direction direction;
    private boolean canShoot = true;
    
    public enum Direction {
        UP, DOWN, LEFT, RIGHT
    }

    public EnemyTank(int startX, int startY, int health) {
        super(startX, startY);
        this.health = health;
        this.direction = Direction.values()[new Random().nextInt(Direction.values().length)];
    }

    public int getHealth() {
        return health;
    }

    public void takeDamage(int damage) {
        health -= damage;
    }

    public boolean isDestroyed() {
        boolean destroyed = health <= 0;
        if (destroyed) {
            canShoot = false;
        }
        return destroyed;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    @Override
    public void draw(Graphics2D g) {
        if (isDestroyed()) {
            return;
        }
        g.setColor(Color.RED);
        g.fillRect(x, y, 50, 50);

        // 绘制血量条
        g.setColor(Color.GREEN);
        g.fillRect(x, y - 10, (int) (50 * ((double) health / 100)), 5);
        g.setColor(Color.BLACK);
        g.drawRect(x, y - 10, 50, 5);
    }

    public Bullet shoot() {
        // 检查是否可以发射子弹
        if (canShoot) {
            canShoot = false;
            Random random = new Random();
            int offsetX = random.nextInt(100) - 50;
            int offsetY = random.nextInt(100) - 50;
            int bulletX = x + 25 + offsetX;
            int bulletY = y + 25 + offsetY;
            double angle = Math.atan2(offsetY, offsetX);
            return new Bullet(bulletX, bulletY, angle, false);
        }
        return null;
    }

    public void resetShooting() {
        canShoot = true;
    }
}
